package constant.milk.cashbook.asset;

import java.math.BigDecimal;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.util.MilkUtil;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class MoneyActivity extends SuperActivity
{
	private static final int OK_FLAG = 0;
	private static final int NO_FLAG = 1;
	
	private static final int NONE_FLAG = 0;
	private static final int PLUS_FLAG = 1;
	private static final int MINUS_FLAG = 2;
	private static final int MULTIPLY_FLAG = 3;
	private static final int DIVISION_FLAG = 4;
	
	public static final String MONEY_DATA = "MONEY_DATA";
	
	private TextView resultTextView;
	private TextView moneyTextView;
	
	private ImageButton button1;
	private ImageButton button2;
	private ImageButton button3;
	private ImageButton button4;
	private ImageButton button5;
	private ImageButton button6;
	private ImageButton button7;
	private ImageButton button8;
	private ImageButton button9;
	private ImageButton button0;
	private ImageButton button00;
	private ImageButton buttonDelete;
	private ImageButton buttonDot;
	private ImageButton buttonPlus;
	private ImageButton buttonMinus;
	private ImageButton buttonMultiply;
	private ImageButton buttonDivision;
	private ImageButton buttonEqual;
	private ImageButton buttonCancel;
	
	private Button checkButton;
	
	private BigDecimal resultData;
	
	private int result;
	
	private int handling;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.money_activity);
		
		setTitle("금액");
		
		setGoneTipButton();
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		resultTextView = (TextView) findViewById(R.id.moneyResultTextView);
		moneyTextView = (TextView) findViewById(R.id.moneyInputTextView);
		
		button1 = (ImageButton) findViewById(R.id.moneyInput1ImageButton);
		button2 = (ImageButton) findViewById(R.id.moneyInput2ImageButton);
		button3 = (ImageButton) findViewById(R.id.moneyInput3ImageButton);
		button4 = (ImageButton) findViewById(R.id.moneyInput4ImageButton);
		button5 = (ImageButton) findViewById(R.id.moneyInput5ImageButton);
		button6 = (ImageButton) findViewById(R.id.moneyInput6ImageButton);
		button7 = (ImageButton) findViewById(R.id.moneyInput7ImageButton);
		button8 = (ImageButton) findViewById(R.id.moneyInput8ImageButton);
		button9 = (ImageButton) findViewById(R.id.moneyInput9ImageButton);
		button0 = (ImageButton) findViewById(R.id.moneyInput0ImageButton);
		button00 = (ImageButton) findViewById(R.id.moneyInput00ImageButton);
		buttonDelete = (ImageButton) findViewById(R.id.moneyInputDeleteImageButton);
		buttonPlus = (ImageButton) findViewById(R.id.moneyInputPlusImageButton);
		buttonMinus = (ImageButton) findViewById(R.id.moneyInputMinusImageButton);
		buttonMultiply = (ImageButton) findViewById(R.id.moneyInputMultiplyImageButton);
		buttonDivision = (ImageButton) findViewById(R.id.moneyInputDivisionImageButton);
		buttonEqual = (ImageButton) findViewById(R.id.moneyInputEqualImageButton);
		buttonDot = (ImageButton) findViewById(R.id.moneyInputDotImageButton);
		buttonCancel = (ImageButton) findViewById(R.id.moneyInputCancelImageButton);
		
		checkButton = (Button) findViewById(R.id.checkButton);
	}

	@Override
	protected void setOnClick()
	{
		button1.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("1");
			}
		});
		
		button2.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("2");
			}
		});
		
		button3.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("3");
			}
		});
		
		button4.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("4");
			}
		});
		
		button5.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("5");
			}
		});
		
		button6.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("6");
			}
		});
		
		button7.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("7");
			}
		});
		
		button8.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("8");
			}
		});
		
		button9.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("9");
			}
		});
		
		button0.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("0");
			}
		});
		
		button00.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInput("00");
			}
		});
		
		buttonDelete.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextDelete();
			}
		});
		
		buttonPlus.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setPlus();
			}
		});
		
		buttonMinus.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setMinus();
			}
		});
		
		buttonMultiply.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setMultiply();
			}
		});
		
		buttonDivision.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setDivide();
			}
		});
		
		buttonDot.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextDot();
			}
		});
		
		buttonEqual.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextEqual();
			}
		});
		
		buttonCancel.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setCancel();
			}
		});
		
		checkButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				
				if (moneyTextView.getText().toString().equals("-"))
				{
					intent.putExtra(MONEY_DATA, MilkUtil.makeStringWithComma("0"));
				}
				else if (result == NONE_FLAG)
				{
					String money = MilkUtil.getCommaDel(moneyTextView.getText().toString());
					
					intent.putExtra(MONEY_DATA, MilkUtil.makeStringWithComma(money));
				}
				else
				{
					intent.putExtra(MONEY_DATA, MilkUtil.makeStringWithComma(resultData.toString()));
				}
				
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}

	@Override
	protected void setData()
	{
		resultData = BigDecimal.ZERO;
		result = NONE_FLAG;
		handling = NO_FLAG;
		
		moneyTextView.setText("0");
		
//		Intent intent = getIntent();
//		String money = intent.getStringExtra(MONEY_DATA);
//		
//		setMoneyData(money);
	}
	
	private void setMoneyData(String money)
	{
		if (money.equals(""))
		{
			return;
		}
		
		moneyTextView.setText(MilkUtil.makeStringWithComma(money));
	}
	
	private void setTextInput(String number)
	{
		if (handling == NO_FLAG)
		{
			String text = moneyTextView.getText().toString();
			
			if (text.indexOf(".") > -1)
			{
				String temp = text.substring(text.indexOf("."));
				
				if (temp.length() > 2)
				{
					MilkUtil.showToast(superContext, "소수점은 2자리까지 가능합니다.");
					return;
				}
			}
			
			text = text + number;
			
			text = MilkUtil.makeStringWithComma(text);
			
			setMoneyData(text);
		}
		else
		{
			setMoneyData(number);
			
			handling = NO_FLAG;
		}
	}
	
	private void setTextDelete()
	{
		String text = moneyTextView.getText().toString();
		
		if (text.length() < 2)
		{
			moneyTextView.setText("0");
			return;
		}
		
		text = text.substring(0, text.length()-1);
		
		text = MilkUtil.makeStringWithComma(text);
		
		setMoneyData(text);
	}
	
	private void setPlus()
	{
		String text = moneyTextView.getText().toString();
		
		if (text.equals("-"))
		{
			return;
		}
		
		setReckoning();
		
		String resultText = resultTextView.getText().toString() + MilkUtil.makeStringWithComma(text) + " ＋ ";
		
		resultTextView.setText(resultText);
		
		result = PLUS_FLAG;
		handling = OK_FLAG;
	}
	
	private void setMinus()
	{
		String text = moneyTextView.getText().toString();
		
		if (text.equals("0") || text.equals("-"))
		{
			moneyTextView.setText("-");
			handling = NO_FLAG;
			return;
		}
		
		setReckoning();
		
		String resultText = resultTextView.getText().toString() + MilkUtil.makeStringWithComma(text) + " － ";
		
		resultTextView.setText(resultText);
		
		result = MINUS_FLAG;
		handling = OK_FLAG;
	}
	
	private void setMultiply()
	{
		String text = moneyTextView.getText().toString();
		
		if (text.equals("-"))
		{
			return;
		}
		
		setReckoning();
		
		String resultText = resultTextView.getText().toString() + MilkUtil.makeStringWithComma(text) + " × ";
		
		resultTextView.setText(resultText);
		
		result = MULTIPLY_FLAG;
		handling = OK_FLAG;
	}
	
	private void setDivide()
	{
		String text = moneyTextView.getText().toString();
		
		if (text.equals("-"))
		{
			return;
		}
		
		if (!setReckoning())
		{
			MilkUtil.showToast(superContext, "0으로 나눌수 없습니다.");
			return;
		}
		
		String resultText = resultTextView.getText().toString() + MilkUtil.makeStringWithComma(text) + " ÷ ";
		
		resultTextView.setText(resultText);
		
		result = DIVISION_FLAG;
		handling = OK_FLAG;
	}
	
	private void setTextDot()
	{
		String text = moneyTextView.getText().toString();
		
		if (text.indexOf(".") > 0)
		{
			return;
		}
		
		text = text + ".";
		
		moneyTextView.setText(text);
	}
	
	private boolean setReckoning()
	{
		String text = moneyTextView.getText().toString();
		
		if (result == NONE_FLAG)
		{
			resultData = new BigDecimal(MilkUtil.getCommaDel(text));
			return true;
		}
		else if (result == PLUS_FLAG)
		{
			resultData = resultData.add(new BigDecimal(MilkUtil.getCommaDel(text)));
		}
		else if (result == MINUS_FLAG)
		{
			resultData = resultData.subtract(new BigDecimal(MilkUtil.getCommaDel(text)));
		}
		else if (result == MULTIPLY_FLAG)
		{
			resultData = resultData.multiply(new BigDecimal(MilkUtil.getCommaDel(text)));
		}
		else if (result == DIVISION_FLAG)
		{
			if (text.equals("0"))
			{
				return false;
			}
			
			resultData = resultData.divide(new BigDecimal(MilkUtil.getCommaDel(text)), 4, BigDecimal.ROUND_DOWN);
		}
		
		moneyTextView.setText(MilkUtil.makeStringWithComma(resultData.toString()));
		
		return true;
	}
	
	private void setTextEqual()
	{
		if (!setReckoning())
		{
			MilkUtil.showToast(superContext, "0으로 나눌수 없습니다.");
			return;
		}
		
		resultData = BigDecimal.ZERO;
		
		resultTextView.setText("");
		
		result= NONE_FLAG;
		handling = OK_FLAG;
	}
	
	private void setCancel()
	{
		resultTextView.setText("");
		
		moneyTextView.setText("0");
		
		resultData = BigDecimal.ZERO;
		
		result= NONE_FLAG;
		handling = OK_FLAG;
	}
	
	@Override
	protected void tipButton()
	{
	}
}
